<template>
  <div class="detail">
    <div
      class="bg"
      :style="{
        backgroundImage: musicStore?.currentMusic.pic
          ? `url(${musicStore?.currentMusic.pic})`
          : 'url(http://p2.music.126.net/XTVHOQ7E7ZlLDvDW3q_xGg==/109951169797627572.jpg)'
      }"
    />
    <div class="content">
      <div class="left">
        <el-row style="width: 100%; color: #fff; cursor: pointer" justify="start" class="mb-3" @click="$router.back()"
          ><el-icon><ArrowLeft /></el-icon><span>返回</span></el-row
        >
        <div class="music-cover-container" @mouseenter="showPlayIcon = true" @mouseleave="showPlayIcon = false">
          <el-image
            :src="musicStore?.currentMusic.pic"
            fit="cover"
            :class="{ 'playing': isPlaying, 'paused': !isPlaying }"
          />
          <!-- 播放/暂停图标覆盖层 -->
          <div class="play-overlay" v-show="showPlayIcon || !isPlaying">
            <div class="play-icon" @click="togglePlayPause">
              <el-icon v-if="isPlaying" :size="40"><VideoPause /></el-icon>
              <el-icon v-else :size="40"><VideoPlay /></el-icon>
            </div>
          </div>
        </div>
        <div class="name">
          <span>{{ musicStore.currentMusic.title }}</span> {{ musicStore.currentMusic.artist }}
        </div>
        <!-- <div class="btn" @click="goDownLoadMusic">下载该歌曲</div> -->
      </div>
      <div class="right">
        <Lyric />
      </div>
    </div>
    <!-- 控制条 -->
    <musicControl />
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from "vue"
import { useSettingsStore } from "@/store/modules/settings"
import { onMounted, onUnmounted } from "vue"
//@ts-ignore
import Lyric from "./component/Lyric.vue"
//@ts-ignore
import musicControl from "./component/musicControl.vue"
import { useMusicStore } from "@/store/modules/music"
import { downLoadMusicAPI } from '@/api/web/music'

const settingsStore = useSettingsStore()
const musicStore = useMusicStore()

// 播放状态
const isPlaying = ref(false)
const showPlayIcon = ref(false)

// 监听播放器状态
watch(() => musicStore.player, (player: any) => {
  if (player) {
    const updatePlayState = () => {
      isPlaying.value = !player.paused
    }

    player.addEventListener('play', updatePlayState)
    player.addEventListener('pause', updatePlayState)

    // 初始化状态
    updatePlayState()
  }
}, { immediate: true })

// 播放/暂停切换
const togglePlayPause = () => {
  if (!musicStore.player) return

  if (isPlaying.value) {
    musicStore.player.pause()
  } else {
    musicStore.player.play()
  }
}

// 下载歌曲需要登录 开摆！
const goDownLoadMusic = async() => {
  // console.log('musicStore',musicStore.currentMusic.id)
  // const data = await downLoadMusicAPI(musicStore.currentMusic.id)
}

onMounted(() => {
  settingsStore.showOrHideCopyright(false)
  musicStore.toggleHide(true)
})

onUnmounted(() => {
  settingsStore.showOrHideCopyright(true)
  musicStore.toggleHide(false)
})
</script>

<style scoped lang="scss">
.detail {
  position: relative;
  .bg {
    width: 100%;
    height: calc(100vh - 115px);
    background-color: red;
    background-repeat: no-repeat;
    background-size: cover;
    filter: blur(1.875rem) brightness(0.8);
    position: absolute;
    top: 0;
    left: 0;
    z-index: -1;
  }
  .content {
    width: 1280px;
    // height: 400px;
    margin: 0 auto;
    padding-top: 70px;
    background-color: transparent;
    display: flex;
    justify-content: center;
    .left {
      flex: 1;
      display: flex;
      flex-direction: column;
      align-items: center;

      //   background-color: green;
      .music-cover-container {
        position: relative;
        width: 300px;
        height: 300px;
        border-radius: 50%;
        border: 5px solid #fff;
        overflow: hidden;
        cursor: pointer;
        transition: all 0.3s;

        &:hover {
          transform: scale(1.05);
          box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
        }

        .el-image {
          width: 100%;
          height: 100%;
          border-radius: 50%;
          transition: all 0.3s;

          &.playing {
            animation: rotate 10s linear infinite;
          }

          &.paused {
            animation: none;
          }

          img {
            border-radius: 50%;
            width: 100%;
            height: 100%;
          }
        }

        .play-overlay {
          position: absolute;
          top: 0;
          left: 0;
          width: 100%;
          height: 100%;
          background: rgba(0, 0, 0, 0.5);
          display: flex;
          align-items: center;
          justify-content: center;
          border-radius: 50%;
          transition: all 0.3s;

          .play-icon {
            color: #fff;
            transition: all 0.3s;

            &:hover {
              transform: scale(1.1);
            }
          }
        }
      }

      .name {
        height: 30px;
        line-height: 30px;
        margin-top: 20px;
        color: #fff;
        span:nth-child(1) {
          font-size: 24px;
          font-weight: 600;
        }
        span:nth-child(2) {
          font-size: 12px;
          color: #eeecec;
        }
      }
      .btn {
        width: 300px;
        height: 60px;
        line-height: 60px;
        // background-color: #fff;
        border-radius: 40px;
        text-align: center;
        margin-top: 30px;
        color: #eeecec;
        cursor: var(--cursor-pointer);
        border: 2px solid #eeecec;
        &:hover {
          color: #fff;
          border: 2px solid #fff;
        }
      }
    }
    .right {
      flex: 1;
      margin-top: 23px;
      //   background-color: #fff;
    }
  }
}

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
</style>
